package com.jt.provider.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.jt.service.ResourceService;
import jdk.internal.dynalink.linker.LinkerServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicLong;

@RestController
@RequestMapping("/provider")
public class ProviderSentinelController {
    @GetMapping("/sentinel01")
    public String doSentinel01(){
        return "sentinel test 01";
    }
    @GetMapping("/sentinel02")
    public String doSentinel02(){
        return "sentinel test 02";
    }
    @Autowired
    private ResourceService resourceService;
    @GetMapping("/sentinel03")
    public String doSentinel03(){
        resourceService.doGetResource();
        return resourceService.doGetResource();
//        return "sentinel 03 test";
    }

    @GetMapping("/sentinel04")
    public String doSentinel04(){
        resourceService.doGetResource();
        return "sentinel 04 test";
    }
       private AtomicLong atomicLong = new AtomicLong(1);
    @GetMapping("/sentinel05")
    public String doSentinel05() throws InterruptedException {
        long count = atomicLong.getAndIncrement();
        if(count%2==0){
            Thread.sleep(200);
            //throw new RuntimeException("服务调用失败");
        }

        return "sentinel 05 test";
    }

    @GetMapping("/sentinel06")
    @SentinelResource("resource")
    public String doSentinel06(Integer id) {

        return "Get Resource By" +id;
    }



}
